﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using GeoWeb.Common;
using GeoWeb.Controls.WebGrid;
using GeoWeb.Demo.DataContext;
using GeoWeb.DataContext;
using Test;
using GeoWeb.Demo.ButtonConfigInfo;

namespace GeoWeb.Demo.MainTemplate.templates_002.web.tab
{
    public partial class tab : System.Web.UI.Page
    {
        DbUtity db = new DbUtity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                BindbtnList();
            }
        }
        /// <summary>
        /// 获取页面按钮事件
        /// </summary>
        private void BindbtnList()
        {
            string TreeNodeId = Request["configNodeId"];
            mainTitle.InnerHtml = "";
            btnCommand.InnerHtml = "";
            if (!string.IsNullOrEmpty(TreeNodeId))
            {
                MenuTreeDal MenuDal = new MenuTreeDal();
                MenuTreeChildNodesDal menuChildDal = new MenuTreeChildNodesDal();
                GeoMenuTree menuEntity = MenuDal.Select(new Guid(TreeNodeId));
                if (menuEntity != null)
                {
                    mainTitle.InnerHtml = menuEntity.MenuTreeName;
                    List<GeoButtonInfo> list = menuChildDal.GetPageButton(TreeNodeId);
                    string btnHtml = "", btnScript = "";
                    foreach (GeoButtonInfo item in list)
                    {
                        btnHtml += item.ButtonHTML;
                        btnScript += item.JavaScript;
                    }
                    this.Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", btnScript, true);
                    btnCommand.InnerHtml = btnHtml;
                }
            }
        }

        protected void WebGrid1_OnWebGridRefresh(object sender, WebGridRefreshEventArgs e)
        {
            string sort = string.Empty;
            if (e.SortName != "" && e.SortOrder != "")
            {
                sort = " order by " + e.SortName + " " + e.SortOrder;
            }

            int total = db.Db.Count<Country>(new WhereClip("1=1"));
            WebGrid1.DataTable = db.Db.FromSql(string.Format(@"select top {0} * from Country where UID not in 
                (select top {1} UID from Country where 1=1 {2}) {3}", e.PageSize, (e.PageIndex - 1) * e.PageSize, sort, sort), null).ToTable() as DataTable;
            WebGrid1.RowCount = total;
        }

        protected void WebGrid1_OnWebGridColumnCreated(object sender, WebGridColumnCreatedEventArgs e)
        {
            if (e.TableType == TableTypes.CenterTable)
            {
                e.Column.Width = 200;
            }
        }

        protected void WebGrid1_WebGridCellRendering(object sender, WebGridCellRenderingEventArgs e)
        {
            if (e.Column.Type == ColumnTypes.Data && e.RowType == RowTypes.Data)
            {
                WebGridDataColumn datacol = e.Column as WebGridDataColumn;
                if (datacol.Field == "Name")
                {
                    e.FormatString = "<span style='color:red;'>" + e.Row[datacol.Field].ToString() + "</span>";
                    e.IsFormat = true;
                }
                if (e.RowIndex == 7)
                {
                    e.FormatString = "<span style='color:blue;font-weight:bold;'>" + e.Row[datacol.Field].ToString() + "</span>";
                    e.IsFormat = true;
                }
            }

            if (e.RowType == RowTypes.Header)
            {
                e.FormatString = "<span style='font-weight:bold;'>" + e.Column.Title + "</span>";
                e.IsFormat = true;
            }
        }
    }
}